/**
 * Created by Administrator on 2017/8/23.
 */
var User = require('../models/user');

exports.login = function *(req, res) {
  var user = req.body;

  try {
    var _user = yield User.findOne(user);
    if (_user) {
      user.userId = _user.userId;
      req.session.user = user;
      res.json({
        status: 0,
        msg: '登录成功！',
        nickName: _user.userName
      });
    } else {
      res.json({
        status: 2,
        msg: '用户名或密码错误！'
      });
    }
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}

exports.logout = function *(req, res) {
  delete req.session.user;
  res.json({
    status: 0,
    msg: '登出成功！'
  })
}

exports.checkLogin = function *(req, res) {
  if (req.session.user) {
    res.json({
      status: 0,
      msg: '用户已登录！',
      userName: req.session.user.userName
    });
  } else {
    res.json({
      status: 10001,
      msg: '用户未登录！'
    });
  }
}

exports.loginPermission = function (req, res, next) {
  if (req.session.user) {
    next();
  } else {
    res.json({
      status: 10001,
      msg: '用户未登录！'
    });
  }
}

exports.cartList = function *(req, res) {
  if (req.session.user) {
    try {
      var _user = yield User.findOne(req.session.user);
      res.json({
        status: 0,
        msg: '',
        result: _user.cartList
      });
    } catch (err) {
      res.json({
        status: 1,
        msg: err.message
      });
    }
  } else {
    res.json({
      status: 10001,
      msg: '用户未登录！'
    });
  }
}

exports.deleteCart = function *(req, res) {
  var goods = req.body;

  try {
    yield User.update({userId: req.session.user.userId}, {$pull: {cartList: {productId: goods.productId}}});
    res.json({
      status: 0,
      msg: '删除成功'
    });
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}

exports.editCart = function *(req, res) {
  var goods = req.body;

  try {
    yield User.update({userId: req.session.user.userId, 'cartList.productId': goods.productId},
      {'cartList.$.productNum': goods.productNum, 'cartList.$.checked': goods.checked});
    res.json({
      status: 0,
      msg: '修改商品成功！'
    });
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}

exports.editAllChecked = function *(req, res) {
  var data = req.body;

  try {
    var _user = yield User.findOne({userId: req.session.user.userId});
    if (_user) {
      _user.cartList.forEach((item => {
        item.checked = data.allChecked;
      }));
      yield _user.save();
      res.json({
        status: 0,
        msg: '修改全选成功！'
      });
    } else {
      res.json({
        status: 1,
        msg: '修改全选失败！'
      });
    }
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}

exports.addressList = function *(req, res) {
  if (req.session.user) {
    try {
      var _user = yield User.findOne(req.session.user);
      res.json({
        status: 0,
        msg: '',
        result: _user.addressList
      });
    } catch (err) {
      res.json({
        status: 1,
        msg: err.message
      });
    }
  } else {
    res.json({
      status: 10001,
      msg: '用户未登录！'
    });
  }
}

exports.setDefaultAddress = function *(req, res) {
  var address = req.body;

  try {
    var _user = yield User.findOne({userId: req.session.user.userId});
    if (_user) {
      _user.addressList.forEach((item) => {
        if (item.addressId == address.addressId) {
          item.isDefault = true;
        } else {
          item.isDefault = false;
        }
      });
      yield _user.save();
      res.json({
        status: 0,
        msg: ''
      });
    } else {
      res.json({
        status: 1,
        msg: '用户不存在'
      });
    }
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}

exports.deleteAddress = function *(req, res) {
  var address = req.body;

  try {
    yield User.update({userId: req.session.user.userId}, {$pull: {addressList: {addressId: address.addressId}}});
    res.json({
      status: 0,
      msg: ''
    });
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}

exports.addAddress = function *(req, res) {
  var address = req.body;
  console.log(address);

  try {
    var _user = yield User.findOne({userId: req.session.user.userId});
    if (_user) {
      if (_user.addressList.length > 0) {
        var last = _user.addressList[_user.addressList.length - 1];
        address.addressId = last.addressId - 0 + 1;
        address.isDefault = true;
        _user.addressList.forEach((item) => {
          item.isDefault = false;
        });
        _user.addressList.push(address);
      } else {
        address.addressId = "100000001";
        address.isDefault = true;
        _user.addressList.push(address);
      }
      yield _user.save();
      res.json({
        status: 0,
        msg: '',
        result: address
      });
    } else {
      res.json({
        status: 1,
        msg: '用户不存在'
      });
    }
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}

exports.getOrder = function *(req, res) {
  try {
    var _user = yield User.findOne({userId: req.session.user.userId});
    if (_user) {
      var order = new Array;
      _user.cartList.forEach((item) => {
        if (item.checked) {
          order.push(item);
        }
      });
      res.json({
        status: 0,
        msg: '',
        result: order
      });
    } else {
      res.json({
        status: 1,
        msg: '用户不存在'
      });
    }
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}

exports.payment = function *(req, res) {
  var data = req.body;

  try {
    var _user = yield User.findOne({userId: req.session.user.userId});
    if (_user) {
      var address = {};
      _user.addressList.forEach((item) => {
        if (item.isDefault) {
          address = item;
        }
      });

      var goodsList = new Array;
      for (var i = _user.cartList.length - 1; i >= 0; i--) {
        if (_user.cartList[i].checked) {
          goodsList.push(_user.cartList[i]);
          _user.cartList.splice(i, 1);
        }
      }

      var order = {
        orderId: new Date().getTime() + parseInt(Math.random() * 10),
        orderTotal: data.orderTotal,
        addressInfo: address,
        goodsList: goodsList,
        orderStatus: '1',
        createDate: new Date()
      };
      _user.orderList.push(order);

      yield _user.save();
      res.json({
        status: 0,
        msg: '',
        result: order.orderId
      });
    } else {
      res.json({
        status: 1,
        msg: '用户不存在'
      });
    }
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}

exports.getOrderTotal = function *(req, res) {
  var data = req.body;

  try {
    var _user = yield User.findOne({userId: req.session.user.userId});
    if (_user) {
      var result = {
        orderId: '订单不存在',
        orderTotal: 0
      }

      _user.orderList.forEach((item) => {
        if (item.orderId == data.orderId) {
          result.orderId = item.orderId;
          result.orderTotal = item.orderTotal;
        }
      });

      res.json({
        status: 0,
        msg: '',
        result: result
      });
    } else {
      res.json({
        status: 1,
        msg: '用户不存在'
      });
    }
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}

exports.getCartCount = function *(req, res) {
  try {
    var _user = yield User.findOne({userId: req.session.user.userId});
    if (_user) {
      var cartCount = 0;

      _user.cartList.forEach((item) => {
        cartCount += item.productNum;
      });

      res.json({
        status: 0,
        msg: '',
        result: cartCount
      });
    } else {
      res.json({
        status: 1,
        msg: '用户不存在'
      });
    }
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    });
  }
}
